﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cn.Edwon.Web.Support;
using System.Reflection;
using Cn.Cellula.Biz.Port;
using Cn.Edwon.Spring;
using Cn.Cellula.Model.Content;
using Cn.Edwon.Web.Validation;
using Cn.Edwon.Web;
using Cn.Cellula.Client;
using Cn.Cellula.Model.System;
using Cn.Cellula.Model.Security;
using Cn.Cellula.Model.Security.Power;

namespace Cn.Cellula.Website.Admin.Item {
    public partial class EditItem : PageSupport {
        String EOKey = null;
        IItemBiz biz = BizFactory.Get<IItemBiz>();
        protected void Page_Load(object sender, EventArgs e) {
            EOKey = Request.QueryString["id"];
            if (!Page.IsPostBack) {
                InitForm();
                if (!String.IsNullOrEmpty(EOKey)) {
                    FillForm(EOKey);
                }
            }
        }
        void InitForm() {
            if (!String.IsNullOrEmpty(Request.QueryString["cid"])) {
                try {
                    TSysCategoryEO cat = BizFactory.Get<ICategoryBiz>().Load<TSysCategoryEO>(Request.QueryString["cid"]);
                    this.hid_CategoryId.Value = cat.CategoryId;
                    this.txt_CategoryName.Text = cat.CategoryName;
                } catch { }
            }
            IUserBiz ubiz = BizFactory.Get<IUserBiz>();
            IList<TSysRoleEO> list = ubiz.LoadAllRoles(this.Power.RoleLevel);
            this.ddl_EditableRole.DataSource = list;
            this.ddl_EditableRole.DataBind();
            this.ddl_EditableRole.Items.Insert(0, new ListItem("", ""));
            this.ddl_VisiableRole.DataSource = list;
            this.ddl_VisiableRole.DataBind();
            this.ddl_VisiableRole.Items.Insert(0, new ListItem("", ""));
        }
        void FillForm(String key) {
            VItemDetailsEO eo = biz.Load<VItemDetailsEO>(key);
            this.txt_CategoryName.Text = eo.CategoryName;
            this.txt_Desc.Text = eo.ItemDesc;
            this.txt_ItemName.Text = eo.ItemName;
            this.txt_ItemNo.Text = eo.ItemNo;
            this.txt_Tags.Text = eo.ItemTags;
            this.hid_CategoryId.Value = eo.CategoryKey;
            IUserBiz ubiz = BizFactory.Get<IUserBiz>();
            try {
                VSysPowerEO peo = ubiz.Load<VSysPowerEO>(eo.ItemKey);
                if (peo != null) {
                    this.ddl_EditableRole.SelectedValue = peo.EditableRoleKey;
                    this.ddl_VisiableRole.SelectedValue = peo.VisiableRoleKey;
                    this.hid_Dept.Value = peo.DeptKey;
                    this.txt_Dept.Text = peo.DeptName;
                }
            } catch { }
        }
        private String Action() {
            Boolean bk = QuickValidator.Execute(Request, this.txt_ItemName.Parent);
            if (!bk) {
                SiteInfo.Send(Page, TxtDef.ERROR, QuickValidator.Message);
                return null;
            }
            TItemEO eo = null;
            if (String.IsNullOrEmpty(EOKey)) {
                eo = new TItemEO();
                eo.ItemDesc = this.txt_Desc.Text;
                eo.ItemName = this.txt_ItemName.Text;
                eo.ItemNo = this.txt_ItemNo.Text;
                eo.ItemTags = this.txt_Tags.Text;
                eo.CategoryKey = this.hid_CategoryId.Value;
                eo.CreateDt = DateTime.Now;
                eo.UpdateDt = DateTime.Now;
                eo.OwnerKey = this.OwnerKey;
                eo.Operator = (this.Actor == null) ? "" : this.Actor.ID;
                Object newKey = biz.Save<TItemEO>(eo);
                BizFactory.Get<IUserBiz>().BindPower(newKey.ToString(), this.ddl_VisiableRole.SelectedValue, this.ddl_EditableRole.SelectedValue, this.hid_Dept.Value, this.Actor.ID);
                BizFactory.Get<ILogBiz>().LogInsert("对象", eo.ItemName, this.Actor, Request);
                return newKey.ToString();
            } else {
                eo = biz.Load<TItemEO>(EOKey);
                eo.ItemDesc = this.txt_Desc.Text;
                eo.ItemName = this.txt_ItemName.Text;
                eo.ItemNo = this.txt_ItemNo.Text;
                eo.ItemTags = this.txt_Tags.Text;
                eo.CategoryKey = this.hid_CategoryId.Value;
                eo.UpdateDt = DateTime.Now;
                eo.Operator = (this.Actor == null) ? "" : this.Actor.ID;
                biz.Update<TItemEO>(eo);
                BizFactory.Get<IUserBiz>().BindPower(eo.ItemKey, this.ddl_VisiableRole.SelectedValue, this.ddl_EditableRole.SelectedValue, this.hid_Dept.Value, this.Actor.ID);
                BizFactory.Get<ILogBiz>().LogUpdate("对象", eo.ItemName, this.Actor, Request);
                return eo.ItemKey;
            }
        }
        protected void btn_Next_Click(object sender, EventArgs e) {
            String itemKey = Action();
            if (itemKey != null)
                Response.Redirect("EditAtl.aspx?id=" + itemKey);
        }
        protected void btn_save_Click(object sender, EventArgs e) {
            String itemKey = Action();
            if (itemKey != null)
                Response.Redirect("ItemView.aspx?id=" + itemKey);
        }

    }
}